/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.preference.ClassDiagramCommonPreferenceConstants;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete a {@link Property}}.
 * 
 * @author Patrick Tessier, Remi Schnekenburger
 */
public class NodePropertyHelper extends NodeHelper {


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.INodeFactory#createGraphNode(org.eclipse.uml2.uml.NamedElement, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphNode(Element umlElement, Diagram diagram) {

		//Debug.debug(this,"Call ModelResourceManager.createGraphNode() pour "+ umlElement.getClass()+ " dans "+diagram.getName(), debugChannel);


		//look for graphNode of the umlElement parent
		ArrayList parentGraphNode=LookForElement.lookForGraphElement(umlElement.getOwner(), diagram, new ArrayList());
		Iterator it =parentGraphNode.iterator();
		ArrayList createdNode=new ArrayList();
		// this an array of graphNode linked to each Class node is 

		while(it.hasNext()){
			//current node
			GraphNode currentNode=(GraphNode)it.next();

			//node creation
			NodeHelper nf= new NodeHelper();
			GraphNode graphNode=nf.createGraphElement(umlElement,currentNode,new Point(20,20),null);
		}
		return createdNode;
	}

	
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateCommand(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if (!(parent instanceof Diagram)) {//the parent must be a graphNode

			if(parent.getSemanticModel()==null){ // the parent must a Feature
				GraphElement gp=parent.getContainer();//look for GreatParent

				if (gp!=null){
					if (!(gp instanceof Diagram)) {//great parent is not a diagram
						if(gp.getSemanticModel()!=null){ //it is linked to an UML element
							Element elt= ((Uml1SemanticModelBridge)gp.getSemanticModel()).getElement();

							if ((elt instanceof Classifier)&& (!(elt instanceof Enumeration))&& (gp.getContained().get(0)==parent)){ //this is the first container of a classifier.
								return true;
							}
						} 
					}
				}
			}
		}
		return false;
	}
	
	///////////////////////////////////////////////////////////////////////////
	// UML management
	///////////////////////////////////////////////////////////////////////////
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  org.eclipse.uml2.uml.Element createUMLElement(String name,  org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"Call NodePropertyFactory.createElement()", channel); //$NON-NLS-1$
		org.eclipse.uml2.uml.Property aproperty=null;
		if (umlParent instanceof org.eclipse.uml2.uml.Class){
			aproperty=((org.eclipse.uml2.uml.Class)umlParent).createOwnedAttribute(name,null);
			aproperty.setIsUnique(false);
		}
		if (umlParent instanceof org.eclipse.uml2.uml.Interface){
			aproperty=((org.eclipse.uml2.uml.Interface)umlParent).createOwnedAttribute(name,null);
			aproperty.setIsUnique(false);
		}
		if (umlParent instanceof org.eclipse.uml2.uml.Association){
			aproperty=((org.eclipse.uml2.uml.Association)umlParent).createOwnedEnd(name, null);
			aproperty.setIsUnique(false);
		}
		if (umlParent instanceof org.eclipse.uml2.uml.DataType){
			aproperty=((org.eclipse.uml2.uml.DataType)umlParent).createOwnedAttribute(name, null);
			aproperty.setIsUnique(false);
		}
		if (umlParent instanceof org.eclipse.uml2.uml.Signal){
			aproperty=((org.eclipse.uml2.uml.Signal)umlParent).createOwnedAttribute(name, null);
			aproperty.setIsUnique(false);
		}
		if(umlParent instanceof Collaboration) {
			aproperty = UMLFactory.eINSTANCE.createProperty();
			aproperty=((org.eclipse.uml2.uml.Collaboration)umlParent).createOwnedAttribute(name, null);
			((org.eclipse.uml2.uml.Collaboration)umlParent).getCollaborationRoles().add(aproperty);
			aproperty.setIsUnique(false);
		}
		if (aproperty != null) {
			aproperty.setAggregation(AggregationKind.NONE_LITERAL); // this has to be set to NONE_LITERAL for current profiles handling 
		}		
		return aproperty;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof org.eclipse.uml2.uml.Class 
				|| umlParent instanceof Interface 
				|| umlParent instanceof Association
				|| umlParent instanceof DataType
				|| umlParent instanceof Signal
				|| umlParent instanceof Collaboration) {
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof org.eclipse.uml2.uml.Class) {
			return ((org.eclipse.uml2.uml.Class)umlParent).getOwnedAttributes().add((Property)element);
		}
		if(umlParent instanceof Interface) {
			return ((Interface)umlParent).getOwnedAttributes().add((Property)element);
		}
		if(umlParent instanceof Association) {
			return ((Association)umlParent).getOwnedEnds().add((Property)element);
		}
		if(umlParent instanceof DataType) {
			return ((DataType)umlParent).getOwnedAttributes().add((Property)element);
		}
		if(umlParent instanceof Signal) {
			return ((Signal)umlParent).getOwnedAttributes().add((Property)element);
		}
		if(umlParent instanceof Collaboration) {
			((Collaboration)umlParent).getOwnedAttributes().add((Property)element);
			return ((Collaboration)umlParent).getCollaborationRoles().add((ConnectableElement)element);
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof org.eclipse.uml2.uml.Class) {
			return ((org.eclipse.uml2.uml.Class)umlParent).getOwnedAttributes().remove(element);
		}
		if(umlParent instanceof Interface) {
			return ((Interface)umlParent).getOwnedAttributes().remove(element);
		}
		if(umlParent instanceof Association) {
			return ((Association)umlParent).getOwnedEnds().remove(element);
		}
		if(umlParent instanceof DataType) {
			return ((DataType)umlParent).getOwnedAttributes().remove(element);
		}
		if(umlParent instanceof Signal) {
			return ((Signal)umlParent).getOwnedAttributes().remove(element);
		}
		if(umlParent instanceof Collaboration) {
			((Collaboration)umlParent).getCollaborationRoles().remove(element);
			return ((Collaboration)umlParent).getOwnedAttributes().remove(element);
			
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "Property_"; //$NON-NLS-1$
	}

	/**
	 * Generic method that returns a new unique name within a namespace.
	 * 
	 * @param umlParent the parent of the element to create
	 * 
	 * @return a distinguisable name within the namespace of the umlParent
	 */
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$

		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if(null == ((Classifier)umlParent).getAttribute(name, null)){
				return name;
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param graphElement 
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {
		//	Get the preference store
		IPreferenceStore store = PapyrusPlugin.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(ClassDiagramCommonPreferenceConstants.getPreferencePropertyBackgroundColor(store));
		graphElement.setBorderColor(ClassDiagramCommonPreferenceConstants.getPreferencePropertyBorderColor(store));
		graphElement.setFontColor(ClassDiagramCommonPreferenceConstants.getPreferencePropertyFontColor(store));
		FontData[] fontdata = ClassDiagramCommonPreferenceConstants.getPreferencePropertyFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}
}